﻿using Subfaculty.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using RepositoriesInterfaces;
using Moq;
using Entities;
using System.Collections.Generic;
using System.Web.Mvc;
using Subfaculty.ViewModels;

namespace TestsSubfaculty
{
    [TestClass]
    public class StudyPlanControllerTest
    {
        private readonly Mock<IStudyPlanItemRepository> SPItemRep;
        private readonly Mock<IDisciplineRepository> DisciplineRep;
        private readonly Mock<IDepartmentRepository> DepartmentRep;
        private readonly Mock<IDirectionRepository> DirectionRep;        
        private IList<StudyPlanItem> SPitemsList;
        private IList<Direction> DirectionList;
        private IList<Department> DepartmentList;
        private IList<Discipline> DisciplineList;

        public StudyPlanControllerTest()
        {
            SPItemRep = new Mock<IStudyPlanItemRepository>();
            DisciplineRep = new Mock<IDisciplineRepository>();
            DepartmentRep = new Mock<IDepartmentRepository>();
            DirectionRep = new Mock<IDirectionRepository>();            
            SPitemsList = new List<StudyPlanItem>();
            DirectionList = new List<Direction>();
            DepartmentList = new List<Department>();
            DisciplineList = new List<Discipline>();
        }
        
        [TestMethod]
        public void GetStudyPlanItemsByChosenDirection_SPIController()
        {
            // 1. Инициализация
            SPItemRep.Setup(x => x.GetByDirectionId(0)).Returns(SPitemsList);
            DirectionRep.Setup(x => x.GetAll()).Returns(DirectionList);

            // 2. Вызов метода
            var SPIController = new StudyPlanController(SPItemRep.Object, DirectionRep.Object, DisciplineRep.Object, DepartmentRep.Object);
            var result = SPIController.Index(0) as ViewResult;

            // 3. Проверка результата
            var returnedModel = (StudyPlanViewModel)result.Model;           
            Assert.AreEqual((IEnumerable<StudyPlanItem>)SPitemsList, returnedModel.StudyPlanItems);
        }

        [TestMethod]
        public void CreateNewStudyPlanItem_SPIController()
        {
           // 1. Инициализация
            DirectionRep.Setup(x => x.GetByID(It.IsAny<int>())).Returns(new Direction());
            DisciplineRep.Setup(x => x.GetByID(It.IsAny<int>())).Returns(new Discipline());
            DepartmentRep.Setup(x => x.GetByID(It.IsAny<int>())).Returns(new Department());
            var SPItemForSave = new StudyPlanItem();
            SPItemRep.Setup(x => x.Save(SPItemForSave));
            var recievedModel = new StudyPlanEditViewModel() { StudyPlan = SPItemForSave };
            
            // 2. Вызов метода
            var SPIController = new StudyPlanController(SPItemRep.Object, DirectionRep.Object, DisciplineRep.Object, DepartmentRep.Object);
            var result = (RedirectToRouteResult) SPIController.Create(recievedModel);
            
            // 3. Проверка результата
            SPItemRep.Verify(x => x.Save(It.IsAny<StudyPlanItem>()), Times.Exactly(1));
            Assert.AreEqual("Index/0", result.RouteValues["action"]);
        }

        [TestMethod]
        public void EditStudyPlanItem_SPIController()
        {
            // 1. Инициализация
            var SPItemForEdit = new StudyPlanItem();
            SPItemRep.Setup(x => x.GetByID(0)).Returns(SPItemForEdit);            
            DisciplineRep.Setup(x => x.GetByID(It.IsAny<int>())).Returns(new Discipline());
            DepartmentRep.Setup(x => x.GetByID(It.IsAny<int>())).Returns(new Department());
            SPItemRep.Setup(x => x.SaveOrUpdate(SPItemForEdit));
            var recievedModel = new StudyPlanEditViewModel() { StudyPlan = SPItemForEdit };

            // 2. Вызов метода
            var SPIController = new StudyPlanController(SPItemRep.Object, DirectionRep.Object, DisciplineRep.Object, DepartmentRep.Object);
            var result = (RedirectToRouteResult)SPIController.Edit(recievedModel);

            // 3. Проверка результата
            SPItemRep.Verify(x => x.SaveOrUpdate(It.IsAny<StudyPlanItem>()), Times.Exactly(1));
            Assert.AreEqual("Index/0", result.RouteValues["action"]);
        }
        
        [TestMethod]
        public void DeleteStudyPlanItem()
        {
            // 1. Инициализация
            var SPItemForDelete = new StudyPlanItem();
            SPItemRep.Setup(x => x.GetByID(0)).Returns(SPItemForDelete);
            SPItemRep.Setup(x => x.Delete(SPItemForDelete));

            // 2. Вызов метода
            var SPIController = new StudyPlanController(SPItemRep.Object, DirectionRep.Object, DisciplineRep.Object, DepartmentRep.Object);
            var result = (RedirectToRouteResult)SPIController.Delete(0);

            // 3. Проверка результата
            SPItemRep.Verify(x => x.Delete(SPItemForDelete), Times.Exactly(1));
            Assert.AreEqual("Index/0", result.RouteValues["action"]);
        }
    }
}
